package mz.matafe.bolao

import grails.test.*

/**
 * Integration Test Class for a Team.
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class TeamIntegrationTests extends GrailsUnitTestCase {

    def group
    def team

    protected void setUp() {
        super.setUp()
        group = new Group(code: "A", name: "Grupo A")
        team = new Team(name: "Brasil")
    }

    protected void tearDown() {
        super.tearDown()
    }

    /**
     * Testing saving
     */
    void testSave() {
        assertNotNull group.save()
        assertNotNull group.id
        group.addToTeams(team)
        group = Group.get(group.id)
        assertEquals 'A',group.code
        assertEquals 1, group.teams.size()
    }

    /**
     * Testing save/update
     */
    void testSaveAndUpdate() {
        assertNotNull group.save()
        group.addToTeams(team)
        team.name = "Africa do Sul"
        group.save()
        group = Group.get(group.id)
        assertEquals 'A',group.code
        assertEquals 'Africa do Sul', group.teams.toList().get(0).name
    }

    /**
     * Testing delete
     */
    void testDelete() {
        assertEquals 0, Team.count()
        group.addToTeams(team)
        assertNotNull group.save()
        assertEquals 1, Team.count()
        team = Team.findByName('Brasil')
        team.delete()
        assertFalse Team.exists(team.id)
        assertEquals 0, Team.count()
    }

}